package com.vm.myview;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.activity.BitmapCache;
import com.vm.activity.DetailActivity;
import com.vm.commons.VMConst;
import com.vm.model.ActiveSoft;
import com.vm.utils.LruImageCache;
import com.vm.utils.MyDisplayImageOptions;
import com.vm.utils.MyVolley;
import com.wwt.app.R;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月27日 下午2:17:00]
 * @Version: [v1.0]
 * 
 */
public class ViewUtils {

	Activity activity;

	private RequestQueue mQueue;

	// ImageListener listener;
	// ImageLoader mImageLoader;

	public ViewUtils(Context context) {
		this.activity = (Activity) context;

		mQueue = Volley.newRequestQueue(context.getApplicationContext());
	}

	/**
	 * 动态创建LinearLayout
	 * 
	 * @param i
	 * @param activeSoft
	 * @param progress
	 *            1.初始化进度条,0不初始化进度条
	 * @return
	 */
	public LinearLayout dyAddView(int i, final ActiveSoft activeSoft,
			int progress) {

		LinearLayout linearLayout = (LinearLayout) activity.getLayoutInflater()
				.inflate(R.layout.found_item, null);
		if (i == 0) {
			// LruImageCache lruImageCache = LruImageCache.instance();
			// mImageLoader = MyVolley.getImageLoader();
		}

		Button btn_download = (Button) linearLayout
				.findViewById(R.id.btn_download);

		TextView tv_softName = (TextView) linearLayout
				.findViewById(R.id.tv_softName);
		TextView tv_download_number = (TextView) linearLayout
				.findViewById(R.id.tv_download_number);
		ImageView iv_softImage = (ImageView) linearLayout
				.findViewById(R.id.iv_softImage);
		TextView tv_softSize = (TextView) linearLayout
				.findViewById(R.id.tv_softSize);
		RatingBar rb_star = (RatingBar) linearLayout.findViewById(R.id.rb_star);
		TextView tv_software_introduction = (TextView) linearLayout
				.findViewById(R.id.tv_software_introduction);

		if (activeSoft.getName() != null) {
			// String softName =
			// StringUtil.getShotName(soft.getName());
			tv_softName.setText(activeSoft.getName());
		}
		// 大小
		if (activeSoft.getSize() != null) {
			float size = activeSoft.getSize();
			DecimalFormat decimalFormat = new DecimalFormat(".00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
			String shotSize = decimalFormat.format(size);// format 返回的是字符串
			tv_softSize.setText(shotSize + "M");
		}
		// 星级
		if (activeSoft.getStar() != null) {
			float fstar = activeSoft.getStar();
			rb_star.setRating(fstar / 2);
		}

		// 隐藏标语
		tv_software_introduction.setVisibility(View.GONE);

		if (activeSoft.getDownload() != null) {
			String download = null;
			if (activeSoft.getDownload() > 10000) {
				download = Integer.toString(activeSoft.getDownload() / 10000)
						+ "."
						+ Integer
								.toString((activeSoft.getDownload() % 10000) / 1000)
						+ "万";
			} else if (activeSoft.getDownload() > 100000000) {
				download = Integer
						.toString(activeSoft.getDownload() / 100000000)
						+ "."
						+ Integer
								.toString((activeSoft.getDownload() % 100000000) / 10000000)
						+ "亿";
			} else {
				download = Integer.toString(activeSoft.getDownload());
			}
			tv_download_number.setText(download);
		}

		ImageLoader.getInstance().displayImage(
				VMConst.URL + activeSoft.getImageUrl(), iv_softImage,
				MyDisplayImageOptions.getInstance());

		OnClickListener ll_listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, DetailActivity.class);

				intent.putExtra("softId", activeSoft.getSoftId());
				activity.startActivityForResult(intent, 10);
			}
		};

		linearLayout.setOnClickListener(ll_listener);

		/**
		 * 加载一些列下载有关的东西
		 */
		if (progress == 1) {
			new FoundDown(activity, linearLayout, activeSoft);
		} else {
			btn_download.setVisibility(View.GONE);
		}
		// downloadService.initProgressBar(soft);
		return linearLayout;
	}

	/**
	 * 动态创建LinearLayout(积分专区)
	 * 
	 * @param i
	 * @param activeSoft
	 * @param progress
	 *            1.初始化进度条,0不初始化进度条
	 * @return
	 */
	public LinearLayout dyAddView2(int i, final ActiveSoft activeSoft,
			int progress) {

		LinearLayout linearLayout = (LinearLayout) activity.getLayoutInflater()
				.inflate(R.layout.present1_ongoing_item, null);
		if (i == 0) {
			// LruImageCache lruImageCache = LruImageCache.instance();
			// mImageLoader = MyVolley.getImageLoader();
		}

		Button btn_download = (Button) linearLayout
				.findViewById(R.id.btn_download);

		TextView tv_softName = (TextView) linearLayout
				.findViewById(R.id.tv_softName);
		TextView tv_download_number = (TextView) linearLayout
				.findViewById(R.id.tv_download_number);
		ImageView iv_softImage = (ImageView) linearLayout
				.findViewById(R.id.iv_softImage);
		TextView tv_softSize = (TextView) linearLayout
				.findViewById(R.id.tv_softSize);
		RatingBar rb_star = (RatingBar) linearLayout.findViewById(R.id.rb_star);

		TextView tv_remaining_points_num = (TextView) linearLayout
				.findViewById(R.id.tv_remaining_points_num);

		TextView percent = (TextView) linearLayout
				.findViewById(R.id.tv_snap_up);

		ProgressBar pgbar_intergral = (ProgressBar) linearLayout
				.findViewById(R.id.progressBar_snap_up);

		if (activeSoft.getName() != null) {
			// String softName =
			// StringUtil.getShotName(soft.getName());
			tv_softName.setText(activeSoft.getName());
		}
		// 大小
		if (activeSoft.getSize() != null) {
			float size = activeSoft.getSize();
			DecimalFormat decimalFormat = new DecimalFormat(".00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
			String shotSize = decimalFormat.format(size);// format 返回的是字符串
			tv_softSize.setText(shotSize + "M");
		}
		// 星级
		if (activeSoft.getStar() != null) {
			float fstar = activeSoft.getStar();
			rb_star.setRating(fstar / 2);
		}

		if (activeSoft.getIntergral() != 0) {
			tv_remaining_points_num.setText(Integer.toString(activeSoft
					.getIntergral() - activeSoft.getUsedIntergral()));
			/**
			 * 当总积分大于已抢积分
			 */
			if (activeSoft.getIntergral() - activeSoft.getUsedIntergral() > 0) {
				percent.setText(Integer.toString(activeSoft.getUsedIntergral()
						* 100 / activeSoft.getIntergral())
						+ "%");
				pgbar_intergral.setMax(activeSoft.getIntergral());
				pgbar_intergral.setProgress(activeSoft.getUsedIntergral());
			}
			// 活动已结束
			else {
				percent.setText("100%");
				pgbar_intergral.setMax(100);
				pgbar_intergral.setProgress(100);
			}

		} else {
			tv_remaining_points_num.setText("0");
			percent.setText("100%");
			pgbar_intergral.setMax(1);
			pgbar_intergral.setProgress(1);
			progress = 0;
		}

		if (activeSoft.getDownload() != null) {
			String download = null;
			if (activeSoft.getDownload() > 10000) {
				download = Integer.toString(activeSoft.getDownload() / 10000)
						+ "."
						+ Integer
								.toString((activeSoft.getDownload() % 10000) / 1000)
						+ "万";
			} else if (activeSoft.getDownload() > 100000000) {
				download = Integer
						.toString(activeSoft.getDownload() / 100000000)
						+ "."
						+ Integer
								.toString((activeSoft.getDownload() % 100000000) / 10000000)
						+ "亿";
			} else {
				download = Integer.toString(activeSoft.getDownload());
			}
			tv_download_number.setText(download);
		}

		ImageLoader.getInstance().displayImage(
				VMConst.URL + activeSoft.getImageUrl(), iv_softImage,
				MyDisplayImageOptions.getInstance());

		OnClickListener ll_listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, DetailActivity.class);

				intent.putExtra("softId", activeSoft.getSoftId());
				intent.putExtra("btnType", activeSoft.getBtnType());
				activity.startActivityForResult(intent, 10);
			}
		};

		linearLayout.setOnClickListener(ll_listener);

		/**
		 * 加载一些列下载有关的东西
		 */
		if (progress == 1) {
			new PresentFoundDown(activity, linearLayout, activeSoft);
		} else {
			btn_download.setVisibility(View.GONE);
		}
		// downloadService.initProgressBar(soft);
		return linearLayout;
	}

}
